//
//  CategoryViewController.m
//  LimitFree
//
//  Created by lijinghua on 16/3/3.
//  Copyright © 2016年 lijinghua. All rights reserved.
//

#import "CategoryViewController.h"
#import "CategoryModel.h"

@interface CategoryViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic)UITableView *tableView;
@property(nonatomic)NSArray *dataSource;

@end

@implementation CategoryViewController

- (id)init{
    if (self = [super init]) {
        self.navigationStyle = LFNavigationBackStyle;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view addSubview:self.tableView];
    [self fetchCategroyData];
}

- (UITableView*)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64)];
        _tableView.dataSource = self;
        _tableView.delegate   = self;
        //_tableView.estimatedRowHeight = 57;
    }
    return _tableView;
}

- (void)fetchCategroyData{
    [[NetDataEngine sharedInstance] requestCategory:^(id respondObject) {
        self.dataSource = [CategoryModel parseResondsData:respondObject];
        [self.tableView reloadData];
    } falied:^(NSError *error) {
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellId"];
    }
    //cell 的设置
    CategoryModel *model = self.dataSource[indexPath.row];
    //当使用系统的cell是，指定占位图片
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.picUrl] placeholderImage:[UIImage imageNamed:@"icon.png"]];
    cell.imageView.layer.cornerRadius = 8;
    cell.imageView.layer.masksToBounds = YES;
    cell.textLabel.text = model.categoryCname;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"共:%@ 限免中: %@",model.categoryCount,model.limited];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CategoryModel *model = self.dataSource[indexPath.row];
    if ([self.delegate respondsToSelector:@selector(category:didSelectCategroyId:)]) {
        [self.delegate category:self didSelectCategroyId:model.categoryId];
        
        //把自己从导航控制器中推出
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 57;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
